package com.zy.oauth.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.db.utils.QueryUtils;
import com.zy.oauth.dto.query.PasswordQueryDto;
import com.zy.oauth.entity.Password;
import com.zy.oauth.mapper.PasswordMapper;
import com.zy.oauth.service.PasswordService;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PasswordServiceImpl extends ServiceImpl<PasswordMapper, Password> implements PasswordService {

    @Override
    public Password getPassword(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        //PasswordQueryDto dto = PasswordCov.INSTANCE.userIdToDto(userId);
        PasswordQueryDto dto = new PasswordQueryDto();
        dto.setUserId(userId);
        return getPassword(dto);
    }

    @Override
    public Password getPassword(PasswordQueryDto passwordQueryDto) {
        QueryWrapper queryWrapper = QueryUtils.getParam(new QueryWrapper<Password>(), passwordQueryDto, PasswordQueryDto.class);


        List<Password> list = list(queryWrapper);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }


    public static final String KEYCOOR = "-";
    public static final String VALUECOOR = "\\|";
    public static final String SPLITCOOR = ";";

    public static void main(String[] args) {
        String value1 = "演员-李连杰|周杰伦;歌手-周杰|周杰伦;歌曲-青花瓷|双截棍;电影-青蜂侠|英雄";
        String value2 = "青花瓷是周杰伦演唱的一首歌曲，由方文山作词。";
        var map = splitting(value1);
        System.out.println(integration(value2, map));
    }

    public static String integration(String value2, Map<String, List<String>> map) {

        for (String key : map.keySet()) {
            String finalValue = value2;
            List<String> valueArr = map.get(key).stream().filter(e -> finalValue.indexOf(e) > -1).collect(Collectors.toList());
            if (valueArr.size() > 0) {
                valueArr.sort((a, b) -> b.length() - a.length());
            }
            if (!CollectionUtils.isEmpty(valueArr)) {
                String e = valueArr.get(0);
                value2 = value2.replace(e, " " + e + "/" + key + " ");
            }

        }
        return value2;
    }

    public static Map<String, List<String>> splitting(String str) {
        Map<String, List<String>> map = new HashMap();
        Arrays.stream(str.split(SPLITCOOR)).forEach(e -> {
            List<String> list = new ArrayList<>();
            list.addAll(Arrays.asList(e.substring(e.indexOf(KEYCOOR) + 1, e.length()).split(VALUECOOR)));
            map.put(e.substring(0, e.indexOf(KEYCOOR)), list);
        });
        return map;
    }

}
